import pymysql
import requests
'Top10主播主播爬取'

# 各分区的接口
urlArr = [
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100022&tagAll=0&page=1",
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100004&tagAll=0&page=1",
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100023&tagAll=0&page=1",
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100002&tagAll=0&page=1"
]
# 各分区名
areaNameArr = ["娱乐天地", "手游休闲", "网游竞技", "单机热游"]


# 接口爬虫
def getInterData(url):
    # 获取接口返回数据（只爬一页就够了）
    res = requests.get(url)
    # 转成json格式
    response = res.json()
    # 获取详细内容
    artists = response['data']['datas']
    return artists


# 获取每一个分区Top10主播的信息
def getAnchorData(artists, areaName):
    # 前10名主播的信息
    top10 = []
    # 排序
    artists.sort(key=lambda x: x["totalCount"], reverse=False)
    # 找出热度前10的
    index = 0
    while index < 10:
        i = artists[index]
        # 房间号
        # roomNumber = i["profileRoom"]
        # pid
        # liveChannel = i["liveChannel"]
        # 房间号（使用liveChannel，同样能搜到）
        roomNumber = i["liveChannel"]
        # 主播昵称
        nick = i["nick"]
        # 热度
        totalHot = i["totalCount"]
        # 标题
        title = i["roomName"]
        anchor_dict = {
            "roomNumber": roomNumber,
            "nick": nick,
            "totalHot": totalHot,
            "title": title,
            "areaName": areaName
        }
        top10.append(anchor_dict)
        index = index + 1
    return top10


# 创建MySQL连接函数（当做固定）
def getMysqlConn():
    conn = pymysql.connect(host='106.13.194.7',
                           port=3306,
                           user='rdsroot',
                           password='abc123456',
                           db='huya',
                           charset='utf8')
    # 创建操作的游标
    cursor = conn.cursor()
    # 设置字符编码及自动提交
    cursor.execute('set names utf8')  # 固定格式
    cursor.execute('set autocommit=1')
    # 设置自动提交
    return conn, cursor


# 保存贡献榜数据
def saveTop10Data(top10):
    conn, cursor = getMysqlConn()
    # sql语句（注意要改回来，删除copy1）
    sql = "insert into t_top10Anchor (id,roomNumber,nick,totalHot,title,areaName) values(REPLACE(UUID(),'-',''),%s,%s,%s,%s,%s)"
    for anchor in top10:
        # 封装成元组进行插入
        add_data = (anchor["roomNumber"], anchor["nick"], anchor["totalHot"], anchor["title"], anchor["areaName"])
        # 这里会把sql语句中的%s替换掉
        cursor.execute(sql, add_data)
    print("mysql插入数据完成")
    # 关闭连接
    cursor.close()
    conn.close()


# 单元测试用
if __name__ == '__main__':
    i = 0
    for url in urlArr:
        artists = getInterData(url)
        top10 = getAnchorData(artists, areaName=areaNameArr[i])
        saveTop10Data(top10)
        i = i + 1
